# -*- coding: UTF-8 -*-

import socket
import time
import traceback
from VideoCapture import Device
import cv2
import threading

# 全局变量
is_sending = False
cli_address = ('', 0)

# 主机地址和端口
host = '127.0.0.1'
port = 10218

# 初始化UDP socket
ser_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ser_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ser_socket.bind((host, port))


# 接收线程类，用于接收客户端发送的消息
class UdpReceiver(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.thread_stop = False

    def run(self):
        while not self.thread_stop:
            # 声明全局变量，接收消息后更改
            global cli_address
            global is_sending
            try:
                message, address = ser_socket.recvfrom(2048)
            except ConnectionResetError as error:
                if error.errno == 10054:
                    print("与"+str(address)+"连接断开!!!!")
                    is_sending = False
                    continue
                traceback.print_exc()
            #     print message,cli_address
            cli_address = address
            if message.decode() == 'startCam':
                is_sending = True
                ser_socket.sendto(bytes('startRcv', encoding="utf8"), cli_address)
            if message.decode() == 'quitCam':
                is_sending = False
                print('quit camera')

    def stop(self):
        self.thread_stop = True


# 创建接收线程
receiveThread = UdpReceiver()
receiveThread.setDaemon(True)  # 该选项设置后使得主线程退出后子线程同时退出
receiveThread.start()

# 初始化摄像头
cam = Device()
cap = cv2.VideoCapture()
cap.open(0)
##cam.setResolution(320, 240)

# 主线程循环，发送视频数据
while 1:
    if is_sending:
        try:
            falg,image = cap.read()
            show = cv2.resize(image, (160, 120))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
            data = show.tostring()
            ser_socket.sendto(data, cli_address)
            time.sleep(0.05)
        except OSError as error:
            print(error.errno)
    else:
        time.sleep(1)

receiveThread.stop()
ser_socket.close()